﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WinnerCMS.BLL;
using WinnerCMS.BLL.Config;
using WinnerCMS.Model;
using WinnerCMS.Common;

#endregion

namespace WinnerCMS.WebUI.Controllers
{
    /*REST属于一种设计风格，REST POST（新增数据），GET（取得数据），PUT（更新数据），DELETE（删除数据）来进行数据库的增删改查，而如果开发人员的应用程式符合REST原则，则它的服务为“REST风格Web服务“也称的RESRful Web API”。*/

    public class ConfigController : ApiController
    {
        [AcceptVerbsAttribute("GET", "POST")]
        public Result Save([FromBody] string data)
        {
            var res = new Result();

            var nc = new List<KeyValue<string, string>>();
            var collect = HttpUtility.UrlDecode(data).Split('&');
            foreach (var item in collect)
            {
                var kv = new KeyValue<string, string>
                {
                    Key = item.Split('=')[0],
                    Value = item.Split('=')[1]
                };
                nc.Add(kv);
            }

            var config = CMSConfig.Instance();
            var ps = config.GetType().GetProperties();
            foreach (var p in ps)
            {
                var category = p.GetCustomAttributes(typeof (CategoryAttribute), true).FirstOrDefault();
                if (category == null)
                {
                    var postback = nc.FirstOrDefault(x => x.Key.EndsWith("WinnerCMS.BLL.CMSConfig_" + p.Name));
                    var value = postback == null ? "false" : postback.Value;
                    if (p.PropertyType.IsArray)
                    {
                        p.SetValue(config, value.Split(new[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries));
                    }
                    else
                    {
                        p.SetValue(config, ConvertHelper.ChangeType(value, p.PropertyType));
                    }
                }
                else
                {
                    var shili = UI.GetInstance(p.Name);
                    foreach (var _p in shili.GetType().GetProperties())
                    {
                        var postback = nc.FirstOrDefault(x => x.Key.EndsWith(p.Name + "_" + _p.Name));
                        var value = postback == null ? "false" : postback.Value;

                        if (_p.PropertyType.IsGenericType)
                        {
                            var list = new List<string>();
                            list.AddRange(value.Split(new[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries));
                            _p.SetValue(shili, list);
                        }
                        else
                        {
                            _p.SetValue(shili, ConvertHelper.ChangeType(value, _p.PropertyType));
                        }
                    }
                }
            }


            return res;
        }

        private void SetValue(object instance, string name)
        {
        }
    }
}